#include "Sem_v2.hpp"
int main(){
    int cnt = 3;
    Semaphore_Director sem_direct;

    //父进程用的
    SemaphoreBulider bulider;
    sem_direct.Organize_Bulid(bulider, CREATE_SEM, 0660, 3, {1,2,3});
    auto fsem = sem_direct.Get_SemaphorePtr(bulider);


    pid_t subid = fork();

    if(subid < 0){
        cerr << "fork error!" << endl;
        exit(errno);
    }

    else if(subid == 0){
        //子进程用
        SemaphoreBulider bd;
        sem_direct.Organize_Bulid(bd, GET_SEM, 0660, 3, {1,2,3});
        auto csem = sem_direct.Get_SemaphorePtr(bd);

        while(cnt--){
            csem->P(0);
            printf("%c", 'C');
            fflush(stdout);
            printf("%c", 'C');
            fflush(stdout);
            sleep(1);
            csem->V(0);
        }
        exit(0);
    }

    else{
        while(cnt--){
            usleep(1500);
            fsem->P(0);
            printf("%c", 'F');
            fflush(stdout);
            printf("%c", 'F');
            fflush(stdout);
            sleep(1);
            fsem->V(0);
        }
        waitpid(subid, nullptr, 0);
        cout << endl;
    }
    return 0;
}